package work.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import work.po.Book;
import work.service.SearchService;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

// 搜索控制器
@Controller
public class SearchController {

    @Autowired
    SearchService searchService;

    @RequestMapping("/search")
    public String search(Model model, String keyword) throws SQLException {
        List<Book> books;
        books = searchService.search(keyword);
        model.addAttribute("books", books);
        return "show";
    }

    @RequestMapping("/searchByBookName")
    public String searchByBookName(Model model, String keyword) throws SQLException {
        List<Book> books;
        books = searchService.searchByBookName(keyword);
        model.addAttribute("books", books);
        return "show";
    }

    @RequestMapping("/searchByAuthorName")
    public String searchByAuthorName(Model model, String keyword) throws SQLException {
        List<Book> books = new ArrayList<>();
        books = searchService.searchByAuthorName(keyword);
        model.addAttribute("books", books);
        return "show";
    }
}
